package com.cardone.platform.authority.service;

import java.util.List;
import java.util.Map;

import lombok.Getter;
import lombok.Setter;

import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import com.cardone.common.dto.PaginationDto;
import com.cardone.context.ContextHolder;
import com.cardone.platform.authority.dao.RoleUserDao;
import com.cardone.platform.authority.dto.RoleUserDto;
import com.google.common.collect.Lists;

/**
 * 角色与用户服务
 *
 * @author yaohaitao
 *
 */
@Getter
@Setter
@Transactional(readOnly = true)
public class RoleUserDefaultService implements RoleUserService {
	@Override
	@Transactional
	public int deleteByIds(final RoleUserDto deleteRoleUser) {
		return ContextHolder.getBean(RoleUserDao.class).deleteByIds(deleteRoleUser);
	}

	@Override
	public <P> P findByCodeOrId(final Class<P> mappedClass, final RoleUserDto findRoleUser) {
		return ContextHolder.getBean(RoleUserDao.class).findByCodeOrId(mappedClass, findRoleUser);
	}

	@Override
	public <P> P findById(final Class<P> mappedClass, final RoleUserDto findRoleUser) {
		return ContextHolder.getBean(RoleUserDao.class).findById(mappedClass, findRoleUser);
	}

	@Override
	public <P> List<P> findList(final Class<P> mappedClass) {
		return ContextHolder.getBean(RoleUserDao.class).findList(mappedClass);
	}

	@Override
	public <P> List<P> findListByLikeCode(final Class<P> mappedClass, final RoleUserDto findListRoleUser) {
		return ContextHolder.getBean(RoleUserDao.class).findListByLikeCode(mappedClass, findListRoleUser);
	}

	@Override
	@Transactional
	public int insertByCode(final RoleUserDto insertRoleUser) {
		return ContextHolder.getBean(RoleUserDao.class).insertByCode(insertRoleUser);
	}

	@Override
	public int insertByRoleCode(final List<String> roleCodeList, final String userId) {
		if (CollectionUtils.isEmpty(roleCodeList)) {
			return 0;
		}

		final List<RoleUserDto> roleUserList = Lists.newArrayList();

		for (final String roleCode : roleCodeList) {
			final RoleUserDto roleUser = RoleUserDto.newRoleUser();

			roleUser.setRoleCode(roleCode);
			roleUser.setUserId(userId);

			roleUserList.add(roleUser);
		}

		return ContextHolder.getBean(RoleUserDao.class).insertByRoleCode(roleUserList);
	}

	@Override
	public <P> PaginationDto<P> paginationByLikeCode(final Class<P> mappedClass, final RoleUserDto paginationRoleUser) {
		return ContextHolder.getBean(RoleUserDao.class).paginationByLikeCode(mappedClass, paginationRoleUser);
	}

	@Override
	public Integer readByCodeNqIdForCount(final RoleUserDto readRoleUser) {
		return ContextHolder.getBean(RoleUserDao.class).readByCodeNqIdForCount(readRoleUser);
	}

	@Override
	@Transactional
	public <P> P saveByIdOrCode(final Class<P> mappedClass, final RoleUserDto saveRoleUser) {
		return ContextHolder.getBean(RoleUserDao.class).saveByIdOrCode(mappedClass, saveRoleUser);
	}

	@Override
	public Map<String, Object> saveUserIdAndRoleIds(final String userId, final String roleIds) {
		return ContextHolder.getBean(RoleUserDao.class).saveUserIdAndRoleIds(userId, roleIds);
	}

	@Override
	@Transactional
	public int updateByCode(final RoleUserDto updateRoleUser) {
		return ContextHolder.getBean(RoleUserDao.class).updateByCode(updateRoleUser);
	}
}